import { Button, Tooltip, InputNumber, message, Select } from 'antd';
import { getColorTextLoadMap, onMouseOutBtn, removeDateHmsStr, renderTableText, tableSorter, tooltipLength, priceFormat } from '@/utils';
import { AppstoreAddOutlined, DeleteOutlined, EditOutlined } from '@ant-design/icons';

/** 物资表格 */
const openMaterielColumns = (openMaterialModalStatus, delSelectRow, pageType, openProperty) => {
  let tableConfig: Record<string, any>[] = [
    // {
    //   title: '物资编号',
    //   dataIndex: 'assetCode',
    //   key: 'assetCode',
    //   width: 110,
    //   fixed: 'left',
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetCode'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '物资名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 110,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '物资类型',
      dataIndex: 'type',
      key: 'type',
      width: 110,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'type'),
      render: (text: string, record: Record<string, any>) => {
        return <span>{text == '0' ? '材料' : text == '1' ? '资产' : ''}</span>;
      },
    },
    {
      title: '物资分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClassName'),
      render: (text: string | number) => renderTableText(text, true),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '状态',
      dataIndex: 'assetStateName',
      key: 'assetStateName',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetStateName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '所属仓库',
    //   dataIndex: 'assetStoreName',
    //   key: 'assetStoreName',
    //   width: 110,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetStoreName'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '数量',
      dataIndex: 'residueNum',
      key: 'residueNum',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'residueNum'),
      render: (text: string) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurementName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价(元)',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '总价(元)',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '调剂数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      width: 130,
      fixed: 'right',
      sorter: (a: any, b: any) => tableSorter(a, b, 'quantityOrArea'),
      render: (text: string | number, record: Record<string, any>) => {
        return (
          <Tooltip title={text}>
            <div>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
  ];
  if (pageType != 'detail') {
    tableConfig?.push({
      title: (
        <div style={{ width: '100%', textAlign: 'center' }}>
          <Button
            type="link"
            size="small"
            icon={<AppstoreAddOutlined />}
            className="manageBtntTxtStyle"
            onMouseOut={onMouseOutBtn}
            onClick={openMaterialModalStatus}
          >
            选择
          </Button>
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 160,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div style={{ display: 'flex' }}>
            {record?.type != '0' && (
              <Button
                type="link"
                size="small"
                icon={<EditOutlined />}
                className={'manageBtntTxtStyle'}
                onMouseOut={onMouseOutBtn}
                onClick={() => openProperty(record)}
              >
                调剂资产
              </Button>
            )}
            <div className="counterpoartDelBtnStyle">
              <Button
                type="link"
                size="small"
                icon={<DeleteOutlined />}
                className={'manageBtntTxtStyle'}
                onMouseOut={onMouseOutBtn}
                onClick={() => delSelectRow(record)}
              >
                删除
              </Button>
            </div>
          </div>
        );
      },
    });
  }
  return tableConfig;
};

/** 表格配置 */
const materialTableConfig = (assetTypeList, changeNum) => {
  return [
    // {
    //   title: '物资编号',
    //   dataIndex: 'assetCode',
    //   key: 'assetCode',
    //   width: 110,
    //   fixed: 'left',
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetCode'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '物资名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 120,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '物资类型',
      dataIndex: 'type',
      key: 'type',
      width: 100,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'type'),
      render: (text: string, record: Record<string, any>) => {
        return <span>{text == '0' ? '材料' : text == '1' ? '资产' : ''}</span>;
      },
    },
    {
      title: '物资分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClassName'),
      render: (text: string | number) => renderTableText(text, true),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '状态',
      dataIndex: 'assetStateName',
      key: 'assetStateName',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetStateName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '所属仓库',
      dataIndex: 'assetStoreName',
      key: 'assetStoreName',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetStoreName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '数量',
      dataIndex: 'residueNum',
      key: 'residueNum',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'residueNum'),
      render: (text: string) => {
        return <span>{text ? priceFormat(text) : `0.0000`}</span>;
      },
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurementName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '总价',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '调剂数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      width: 160,
      fixed: 'right',
      sorter: (a: any, b: any) => tableSorter(a, b, 'quantityOrArea'),
      render: (text: number | string, record: Record<string, any>) => {
        const MaxNum: number = Number(record?.residueNum || 0);
        // 控制当数量大于当前可选数量时显示
        let ishowMax = Number(text) > MaxNum ? true : false;
        let ishowMin = Number(text) <= 0;
        if (MaxNum <= 0) {
          return <div style={{ textAlign: 'center', padding: '0 11px' }}>暂无材料数量</div>;
        } else {
          return (
            <>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                <InputNumber
                  status={ishowMax || ishowMin ? 'error' : ''}
                  bordered={ishowMax || ishowMin ? true : false}
                  value={text}
                  style={{ width: '100%' }}
                  onChange={(value: any) => {
                    // if (Number(value) > MaxNum || Number(value) <= 0) {
                    //   setisNextBtnFun(true);
                    // } else {
                    //   setisNextBtnFun(false);
                    // }

                    if (Number(value) > Number(record.residueNum)) {
                      message.error('材料数量不足！');
                      return;
                    } else {
                      changeNum(record.materialOrAssetId, value);
                    }
                  }}
                  min={1}
                  max={MaxNum}
                  disabled={text == 0}
                  precision={4}
                />
              </div>
              {ishowMax && <div style={{ paddingLeft: '5px', color: 'red' }}>{`最多可选择的数量为${MaxNum}`}</div>}
              {ishowMin && <div style={{ paddingLeft: '5px', color: 'red' }}>{`最少选择的数量为1`}</div>}
            </>
          );
        }
      },
    },
  ];
};

/** 调剂资产表格配置 */
const propertyTableConfig = () => {
  return [
    {
      title: '资产编号',
      dataIndex: 'assetCode',
      key: 'assetCode',
      width: 160,
      fixed: 'left',
      // width: 110,
      // fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetCode'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '资产名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 160,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 200,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '总价',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 200,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
  ];
};

export { materialTableConfig, openMaterielColumns, propertyTableConfig };
